﻿function invalidUser() {
	$('#registeration').animate({'height':'285px'});
	$('#invalidusername').animate({'height':'20px', 'opacity':'1.0'});
}

function invalidEmail() {
	$('#registeration').animate({'height':'285px'});
	$('#invalidemail').animate({'height':'20px', 'opacity':'1.0'});
}

function validateForm() {
	$('#invalidusername, #invalidemail').css({'opacity':'0.0', 'height':'0'});
	$('#registeration').css('height','260px');
	var user = $('#register-username').val()
	,	email = $('#register-email').val()
	,	atpos = email.indexOf('@')
	,	dotpos = email.lastIndexOf('.');
	if (user == '' || user == null || user.length > 65 || user == 'Desired username') {
		invalidUser();
		return false;
	}
	if (atpos < 1 || dotpos < atpos+2 || dotpos+2 > email.length || email == '' || email == null || email.length > 65 || email == 'Your e-mail address') {
		invalidEmail();
		return false;
	}
	return true;
}

$(window).resize(function() {
	$('#overlay').css({'width':$(window).width(),'height':$(window).height()});
	$('#login, #registeration').css({'top':'260px', 'left':($(window).width()/2)-($('#login').width()/2)-5+'px'});
});


$(document).ready(function() {
	$('#overlay').css({'width':$(window).width(),'height':$(window).height()});
	$('#login, #registeration').css({'top':'260px', 'left':($(window).width()/2)-($('#login').width()/2)-5+'px'});
	
	//Fancy login & register
	if (typeof(loginError) != 'undefined') {
		$('#badlogin').animate({'opacity':'1.0','height':'20'});
		$('#login').css('display','block').animate({'height':'240'});
		$('#overlay').css('display','block');
	}
	if (typeof(registerError) != 'undefined') {
		$('#badregister').animate({'opacity':'1.0','height':'20'}).html(registerError);
		$('#registeration').css('display','block').animate({'height':'290'});
		$('#overlay').css('display','block');
	}
	$('#login-button, .bubble').mousedown(function() {
		$('#overlay, #login').fadeIn();
	});
	$('#register-button').mousedown(function() {
		$('#overlay, #registeration').fadeIn();
	});
	$('#logout-button').mousedown(function() {
		window.location = '/logout';
	});
	$('.close-button').mousedown(function() {
		$('#overlay, #login, #registeration').fadeOut();
	});
	if (typeof(loggedIn) != 'undefined') {
		$('#login-button, #or, #register-button').css('display','none');
		$('#logout-button').css('display','inline')
		$('#loginname').html(loggedIn);
	}
	$('input').focusin(function() {
		if ($(this).val() == 'Username' || $(this).val() == 'Desired username' || $(this).val() == 'Password' || $(this).val() == 'Your e-mail address') {
			justincase = $(this).val();
			$(this).val('');
		}
	})
	.focusout(function() {
		if ($(this).val() == '') $(this).val(justincase);
	});

	//Preload images
	img1 = new Image();
	img2 = new Image();
	img3 = new Image();
	img4 = new Image();

	img1.src = "/images/iconmonstr-speech-bubble-14-icon.png";
	img2.src = "/images/iconmonstr-speech-bubble-14-icon-hover.png";
	img3.src = "/images/iconmonstr-x-mark-4-icon.png";
	img4.src = "/images/bg.png";
});
